<?php

/**
 * This is the model class for table "{{organisation}}".
 *
 * The followings are the available columns in table '{{organisation}}':
 * @property integer $org_id
 * @property string $vorname
 * @property string $nachname
 * @property string $ntname
 * @property string $anrede
 * @property integer $parent_org_id
 * @property integer $type
 * @property integer $is_group
 *
 * The followings are the available model relations:
 * @property Kommunikation[] $kommunikations
 * @property OrganisationHasTblOrganisation[] $organisationHasTblOrganisations
 * @property OrganisationHasTblOrganisation[] $organisationHasTblOrganisations1
 * @property Recipient[] $recipients
 */
class Organisation extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Organisation the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{organisation}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('parent_org_id, type, is_group', 'numerical', 'integerOnly'=>true),
			array('vorname, nachname, anrede', 'length', 'max'=>45),
			array('ntname', 'length', 'max'=>100),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('org_id, vorname, nachname, ntname, anrede, parent_org_id, type, is_group', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'kommunikations' => array(self::HAS_MANY, 'Kommunikation', 'org_id'),
			'organisationHasTblOrganisations' => array(self::HAS_MANY, 'OrganisationHasTblOrganisation', 'tbl_organisation_org_id','together'=>true),
			'organisationHasTblOrganisations1' => array(self::HAS_MANY, 'OrganisationHasTblOrganisation', 'tbl_organisation_org_childid','together'=>true),
			'recipients' => array(self::HAS_MANY, 'Recipient', 'org_id','together'=>true),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'org_id' => 'Org',
			'vorname' => 'Prename',
			'nachname' => 'Name',
			'ntname' => 'Ntname',
			'anrede' => 'Salutation',
			'parent_org_id' => 'Parent Org',
			'type' => 'Type',
			'is_group' => 'Is Group',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('org_id',$this->org_id);
		$criteria->compare('LOWER(vorname)',strtolower($this->vorname),true);
		$criteria->compare('LOWER(nachname)',strtolower($this->nachname),true);
		$criteria->compare('ntname',$this->ntname,true);
		$criteria->compare('anrede',$this->anrede,true);
		$criteria->compare('parent_org_id',$this->parent_org_id);
		$criteria->compare('type',$this->type);
		$criteria->compare('is_group',$this->is_group);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
}